package com.stone.stoneanswer.model.vo;

import cn.hutool.json.JSONUtil;
import com.stone.stoneanswer.model.entity.QuestionOption;
import com.stone.stoneanswer.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * 题目
 * @TableName question
 */
@Data
public class QuestionVO {

    /**
     * Id
     */
    private Long id;
    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目类型，0-选择题，1-判断题，2-问答题
     */
    private Integer type;

    /**
     * 题目选项列表
     */
    private List<QuestionOption> options;

    /**
     * 题目答案（问答题）
     */
    private String answer;

    /**
     * 是否正确（判断题）
     */
    private Integer isCorrect;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 是否已经点赞
     */
    private Boolean isThumb;

    /**
     * 是否已经收藏
     */
    private Boolean isFavour;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建人信息
     */
    private UserVO user;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO  , question);
        List<String> tagList = questionVO.getTags();
        question.setTags(JSONUtil.toJsonStr(tagList));
        List<QuestionOption> optionList = questionVO.getOptions();
        question.setOptions(JSONUtil.toJsonStr(optionList));
        return question;
    }

    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        questionVO.setTags(JSONUtil.toList(question.getTags(), String.class));
        questionVO.setOptions(JSONUtil.toList(question.getOptions() , QuestionOption.class));
        return questionVO;
    }
}